﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using M7;
using System.Threading;
using System.IO;

namespace S7.Windows.Music
{
    /// <summary>
    /// Interaction logic for EditMusic.xaml
    /// </summary>
    public partial class EditMusic : Window
    {
        public EditMusic()
        {
            InitializeComponent();
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            NSong nSong = (NSong)lbSongs.SelectedValue;

            var songs = from song in Song.lstSongs
                        from artist in Artist.lstArtists
                        where song.SongName.Equals(nSong.Name)
                        && artist.ArtistName.Equals(nSong.Artist)
                        && song.ArtistID.Value.Equals(artist.ArtistID)
                        select song;

            Thread thrDB = new Thread(new ThreadStart(() =>
                {
                    try
                    {
                        foreach (var song in songs)
                        {
                            song.Delete();
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                    }
                }));
            thrDB.IsBackground = true;
            thrDB.Start();

            Thread thrDir = new Thread(new ThreadStart(() =>
            {
                try
                {
                    foreach (var song in songs)
                    {
                        if (File.Exists(song.SongPath))
                        {
                            File.Delete(song.SongPath);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                }
            }));
            thrDir.IsBackground = true;
            thrDir.Start();
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            var w = new AddMusic();
            w.ShowDialog();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Thread thr = new Thread(new ThreadStart(() =>
            {
                try
                {
                    Genre.lstGenre = Genre.Search();
                    lbGenres.Dispatcher.Invoke(new ThreadStart(() =>
                    {
                        try
                        {
                            lbGenres.Items.Add("All");
                            foreach (var genre in Genre.lstGenre)
                            {
                                lbGenres.Items.Add(genre.GenreName);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                        }
                    }));
                    Artist.lstArtists = Artist.Search();
                    Song.lstSongs = Song.Search();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                }
            }));
            thr.IsBackground = true;
            thr.Start();
        }

        private void lbGenres_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string selectedGenre = lbGenres.SelectedItem.ToString();
            Thread thr = new Thread(new ThreadStart(() =>
            {
                try
                {
                    while (true)
                    {
                        if (Song.lstSongs != null && Artist.lstArtists != null)
                        {
                            if (selectedGenre.Equals("All"))
                            {
                                var songs = from song in Song.lstSongs
                                            from artist in Artist.lstArtists
                                            where song.ArtistID == artist.ArtistID
                                            select new { song.SongName, artist.ArtistName };
                                List<NSong> lstNsongs = new List<NSong>();
                                foreach (var NSong in songs)
                                {
                                    NSong song = new NSong();
                                    song.Artist = NSong.ArtistName;
                                    song.Name = NSong.SongName;
                                    lstNsongs.Add(song);
                                }
                                lbSongs.Dispatcher.Invoke(new ThreadStart(() =>
                                {
                                    try
                                    {
                                        lbSongs.ItemsSource = lstNsongs;
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                                    }
                                }));
                            }
                            else
                            {
                                var songs = from song in Song.lstSongs
                                            from genre in Genre.lstGenre
                                            from artist in Artist.lstArtists
                                            where song.GenreID == genre.GenreID && genre.GenreName.Equals(selectedGenre)
                                            && song.ArtistID == artist.ArtistID
                                            select new { song.SongName, artist.ArtistName };
                                List<NSong> lstResult = new List<NSong>();
                                foreach (var song in songs)
                                {
                                    lstResult.Add(new NSong { Name = song.SongName, Artist = song.ArtistName });
                                }
                                lbSongs.Dispatcher.Invoke(new ThreadStart(() =>
                                {
                                    try
                                    {
                                        lbSongs.ItemsSource = lstResult;
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                                    }
                                }));
                            }
                            break;
                        }
                        else
                        {
                            Thread.Sleep(50);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                }
            }));
            thr.IsBackground = true;
            thr.Start();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Song.lstSongs = null;
            Artist.lstArtists = null;
            Genre.lstGenre = null;
        }

        private void tbSearch_KeyUp(object sender, KeyEventArgs e)
        {
            string Text = tbSearch.Text;
            if (Text.Equals(""))
            {
                while (true)
                {
                    if (Artist.lstArtists != null || Song.lstSongs != null)
                    {
                        var songs = from song in Song.lstSongs
                                    from artist in Artist.lstArtists
                                    where song.ArtistID == artist.ArtistID
                                    select new { song.SongName, artist.ArtistName };
                        List<NSong> lstNsongs = new List<NSong>();
                        foreach (var NSong in songs)
                        {
                            NSong song = new NSong();
                            song.Artist = NSong.ArtistName;
                            song.Name = NSong.SongName;
                            lstNsongs.Add(song);
                        }
                        lbSongs.ItemsSource = lstNsongs;
                        break;
                    }
                    else
                    {
                        Thread.Sleep(50);
                    }
                }
            }
            else
            {
                Thread thrSearch = new Thread(new ThreadStart(() =>
                {
                    while (true)
                    {
                        if (Song.lstSongs == null || Artist.lstArtists == null)
                        {
                            Thread.Sleep(50);
                        }
                        else
                        {
                            List<Song> lstSongs = S7.Music.SearchText(Text);
                            List<NSong> lstNsong = new List<NSong>();
                            foreach (var song in lstSongs)
                            {
                                var q = from artist in Artist.lstArtists
                                        where song.ArtistID == artist.ArtistID
                                        select artist;
                                NSong nSong = new NSong();
                                nSong.Name = song.SongName;
                                List<Artist> artists = q.ToList();
                                if (artists.Count > 0)
                                {
                                    nSong.Artist = artists[0].ArtistName;
                                }
                                lstNsong.Add(nSong);
                            }
                            lbSongs.Dispatcher.Invoke(new ThreadStart(() =>
                            {
                                lbSongs.ItemsSource = lstNsong;
                            }), null);
                            break;
                        }
                    }
                }));
                thrSearch.IsBackground = true;
                thrSearch.Start();
            }
        }

        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
